from .base_model import BaseModel
from django.contrib.auth.models import User
from .role import Role
from .semester import Semester
from django.db import models

class UserRoleAssignment(BaseModel):
    """用户角色分配，支持按学期分配不同角色"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='role_assignments', verbose_name="用户")
    role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name='assignments', verbose_name="角色")
    semester = models.ForeignKey(Semester, on_delete=models.CASCADE, related_name='role_assignments', verbose_name="学期")
    is_active = models.BooleanField(default=True, verbose_name="是否活跃")
    assigned_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='assigned_roles', verbose_name="分配者")

    def __str__(self):
        return f"{self.user.profile.real_name} - {self.role.get_name_display()} ({self.semester})"

    class Meta:
        verbose_name = "用户角色分配"
        verbose_name_plural = "用户角色分配"
        unique_together = ('user', 'role', 'semester')